package com.wzh.glsc.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.common.constant.WareConstant;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.Query;
import com.wzh.glsc.ware.dao.PurchaseDao;
import com.wzh.glsc.ware.entity.PurchaseDetailEntity;
import com.wzh.glsc.ware.entity.PurchaseEntity;
import com.wzh.glsc.ware.service.PurchaseDetailService;
import com.wzh.glsc.ware.service.PurchaseService;
import com.wzh.glsc.ware.service.WareSkuService;
import com.wzh.glsc.ware.vo.MergeVo;
import com.wzh.glsc.ware.vo.PurchaseDoneItemsVo;
import com.wzh.glsc.ware.vo.PurchaseDoneVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Resource
    private PurchaseDetailService purchaseDetailService;
    @Resource
    private WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryUnreceive(Map<String, Object> params) {
        IPage<PurchaseEntity> purchaseEntityIPage=this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
                        .eq("status", WareConstant.PurchaseStatusEnum.NEW.getCode())
                        .or().eq("status", WareConstant.PurchaseStatusEnum.ALLOCATED.getCode()));
        return new PageUtils(purchaseEntityIPage);
    }

    @Override
    public void saveMerge(MergeVo mergeVo) {
        QueryWrapper<PurchaseEntity> purchaseEntityQueryWrapper = new QueryWrapper<>();
        Long purchaseId=mergeVo.getPurchaseId();
        if(purchaseId==null){
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.NEW.getCode());
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            save(purchaseEntity);
            purchaseId=purchaseEntity.getId();
        }
        //TODO 判断 是否满足合并条件，status在创建或者分配状态下


        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> purchaseDetailEntities= Arrays.asList(mergeVo.getItems()).stream().map((obj)->{
            PurchaseDetailEntity purchaseDetailEntity=new PurchaseDetailEntity();
            purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailsEnum.ALLOCATED.getCode());
            purchaseDetailEntity.setPurchaseId(finalPurchaseId);
            purchaseDetailEntity.setId(obj);
            return purchaseDetailEntity;
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(purchaseDetailEntities);
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(finalPurchaseId);
        purchaseEntity.setUpdateTime(new Date());
        updateById(purchaseEntity);
    }

    @Transactional
    @Override
    public void receivedPurchaseByIds(Long[] ids) {
        List<PurchaseEntity> purchaseEntities=Arrays.asList(ids).stream().map((id)->{
            PurchaseEntity purchaseEntity=getById(id);
            return  purchaseEntity;
        }).filter(purchaseEntity -> purchaseEntity.getStatus()==WareConstant.PurchaseStatusEnum.NEW.getCode()
                ||purchaseEntity.getStatus()==WareConstant.PurchaseStatusEnum.ALLOCATED.getCode()
        ).map(purchaseEntity->{
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.ALREADY_RECEIVED.getCode());
            purchaseDetailService.updateStatusByPurchaseId(purchaseEntity.getId());
            return purchaseEntity;
        }).collect(Collectors.toList());
        updateBatchById(purchaseEntities);
    }

    @Transactional
    @Override
    public void purchaseDone(PurchaseDoneVo purchaseDoneVo) {
        Boolean flag=true;
        List<PurchaseDetailEntity> purchaseDetailEntities=new ArrayList<>();
        if(baseMapper.selectById(purchaseDoneVo.getId())==null){
            return;
        }
        for ( PurchaseDoneItemsVo itemsVo: purchaseDoneVo.getItems() ) {
            if(itemsVo.getStatus()==WareConstant.PurchaseDetailsEnum.PURCHASE_FAILED.getCode()){
                flag=false;
            }
            PurchaseDetailEntity purchaseDetailEntity=new PurchaseDetailEntity();
            purchaseDetailEntity.setId(itemsVo.getItemId());
            purchaseDetailEntity.setStatus(itemsVo.getStatus());
            purchaseDetailEntities.add(purchaseDetailEntity);
            PurchaseDetailEntity savePurchaseDetailEntity=purchaseDetailService.getById(purchaseDetailEntity.getId());
            wareSkuService.saveWareSkuByPurchaseDetailEntity(savePurchaseDetailEntity);
        }
        purchaseDetailService.updateBatchById(purchaseDetailEntities);

        PurchaseEntity purchaseEntity=new PurchaseEntity();
        purchaseEntity.setId(purchaseDoneVo.getId());
        purchaseEntity.setStatus(flag?WareConstant.PurchaseStatusEnum.OFF_THE_STOCKS.getCode()
                :WareConstant.PurchaseStatusEnum.THERE_IS_ABNORMAL.getCode());
        purchaseEntity.setUpdateTime(new Date());
        baseMapper.updateById(purchaseEntity);
    }
}